<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_receive_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }


    public function get_items(){
        $sql = "SELECT * FROM inv_items where enable_flag = 'Y' ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    
    }
    public function get_sub_inv(){
      $sql = "SELECT * FROM inv_sub";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    }

    public function get_locators($id){
      $sql = "SELECT * FROM inv_sub_locator where inv_sub_id = ".$id." ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    }

    public function get_trans_type(){
      $sql = "SELECT * FROM app_lookup_values where lookup_type = 'INV_TRANSACT_TYPE' ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    }

    public function insert_trans_inv($data){
      $this->db->query("call invTrans_rcv_ins(?,?,?,?,?,?,@err,@msg)",$data);
      $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
      echo json_encode($que->result());
    }

    public function search_trans($item_num){
        if($item_num != ""){
            $where = "WHERE ii.item_num LIKE ".$this->db->escape($item_num)." AND trans_type = 'INV_RCV' ";
          }else{
            $where = "WHERE ii.item_num LIKE 'xasjdnaqeqsa' AND trans_type = 'INV_RCV' ";
          }

      $sql = "SELECT it.transact_id trans_id
                    ,ii.item_num item_num
                    ,ii.description i_desc
                    ,iis.inv_sub_code sub_code
                    ,iis.inv_sub_name sub_name
                    ,isl.locator_code loc_code
                    ,isl.locator_description loc_desc
                  FROM inv_transactions it
                    INNER JOIN inv_items ii ON it.item_id = ii.item_id
                    INNER JOIN inv_sub iis ON iis.inv_sub_id = it.inv_sub_id
                    INNER JOIN inv_sub_locator isl ON isl.locator_id = it.locator_id
                  ".$where." 
                ";
       $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }

       
    }


    public function get_trans_data($id){
       $sql = "SELECT it.transact_id trans_id
                    ,ii.item_num item_num
                    ,ii.description i_desc
                    ,iis.inv_sub_code sub_code
                    ,iis.inv_sub_name sub_name
                    ,isl.locator_code loc_code
                    ,isl.locator_description loc_desc
                    ,it.item_qty item_qty
                    ,it.reorder_lvl reorder_lvl
                  FROM inv_transactions it
                    INNER JOIN inv_items ii ON it.item_id = ii.item_id
                    INNER JOIN inv_sub iis ON iis.inv_sub_id = it.inv_sub_id
                    INNER JOIN inv_sub_locator isl ON isl.locator_id = it.locator_id where it.transact_id = ".$id." ";
       // $sql="SELECT urs.line_id line_id, urs.rate_id rate_id, uce.charge_name charge_name, urs.description description, urs.usage_qty usage_qty, urs.rate rate FROM ubs_rate_schedule urs INNER JOIN ubs_chg_elements uce ON uce.charge_code = urs.charge_code WHERE urs.rate_id = '".$id."' ";
        $query = $this->db->query($sql);
        return $query->result();
    }
}